﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ThreeTeams.Infrastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        private readonly MyContext context;

        public BaseRepository(MyContext context)
        {
            this.context = context;
        }

        public async Task<int> Create(T entity)
        {
            await context.Set<T>().AddAsync(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> CreateRange(IEnumerable<T> entities)
        {
            await context.Set<T>().AddRangeAsync(entities);
            return await context.SaveChangesAsync();
        }

        public async Task<int> Deleted(T entity)
        {
            context.Set<T>().Remove(entity);
            return await context.SaveChangesAsync();
        }

        public async Task<int> DeletedRange(IEnumerable<T> entities)
        {
            context.Set<T>().RemoveRange(entities);
            return await context.SaveChangesAsync();
        }

        public async Task<int> DeletedRange(Expression<Func<T, bool>> expression)
        {
            var list = context.Set<T>().Where(expression);
            context.Set<T>().RemoveRange(list);
            return await context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            return context.Set<T>();
        }

        public async Task<int> Update(T entity)
        {
            context.Set<T>().Update(entity);
            return await context.SaveChangesAsync();
        }
    }
}
